﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DAL;
using Entities;
using DevExpress.XtraReports.UI;

namespace QLTTTH
{
    public partial class UCHocPhiBC : DevExpress.XtraEditors.XtraUserControl
    {
        public enum DKTimHocPhi
        {
            TheoNhanVien,
            TheoHocVien,
            TheoNgay
        }

        private DKTimHocPhi dieuKien;
        public bool isStatistics = false;

        public UCHocPhiBC()
        {
            InitializeComponent();
        }

        private void UCHocPhiBC_Load(object sender, EventArgs e)
        {
            LoadDsHocPhi();
            InitControl();
        }

        private void LoadDsHocPhi()
        {
            var dsHP = HocPhiManager.LayDanhSachBienLai();
            if (dsHP.Success)
            {
                LoadDsHocPhi(dsHP);
            }
        }

        private void LoadDsHocPhi(ActionResultAsList<BienLai> dsHP)
        {
            dgvHocPhi.DataSource = dsHP.Result;
        }

        private void InitControl()
        {
            var dsNV = NhanVienManager.TimDanhSachNhanVien("Kế toán", 
                (object a, object b) =>
                {
                    return (b as NguoiDung).ChucVu.CompareTo(a);
                });
            if (dsNV.Success)
            {
                loupNhanVien.Properties.DataSource = dsNV.Result;
                loupNhanVien.Properties.ValueMember = "MaNV";
                loupNhanVien.Properties.DisplayMember = "HoTen";

            }

            var dsHV = HocVienManager.LayDanhSachHocVien();
            if (dsHV.Success)
            {
                loupHocVien.Properties.DataSource = dsHV.Result;
                loupHocVien.Properties.ValueMember = "MaHV";
                loupHocVien.Properties.DisplayMember = "HoTen";
            }
        }

        private void loupNhanVien_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimHocPhi.TheoNhanVien;
            isStatistics = true;
        }

        private void loupHocVien_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimHocPhi.TheoHocVien;
            isStatistics = true;
        }

        private void dtpTuNgay_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimHocPhi.TheoNgay;
            dtpDenNgay.EditValue = dtpTuNgay.EditValue;
            isStatistics = true;
        }

        private void dtpDenNgay_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimHocPhi.TheoNgay;
            isStatistics = true;
        }

        private void btnThongKe_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (!isStatistics)
            {
                XtraMessageBox.Show("Chưa chọn nội dung thống kê", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ActionResultAsList<BienLai> aResult;
            if (!string.IsNullOrEmpty(dieuKien.ToString()))
            {
                switch (dieuKien)
                {
                    case DKTimHocPhi.TheoNhanVien:
                        aResult = HocPhiManager.TimDanhSachHocPhi(loupNhanVien.EditValue,
                            (object a, object b) =>
                            {
                                return (b as BienLai).MaNV.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsHocPhi(aResult);
                        }
                        break;
                    case DKTimHocPhi.TheoHocVien:
                        aResult = HocPhiManager.TimDanhSachHocPhi(loupHocVien.EditValue,
                            (object a, object b) =>
                            {
                                return (b as BienLai).MaHV.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsHocPhi(aResult);
                        }
                        break;
                    case DKTimHocPhi.TheoNgay:
                        aResult = HocPhiManager.TimDanhSachHocPhiTheoNgay(dtpTuNgay.EditValue, dtpDenNgay.EditValue,
                            (object a, object b, object c) =>
                            {
                                return (c as BienLai).NgayThu.CompareTo(a) >= 0 && (c as BienLai).NgayThu.CompareTo(b) <= 0;
                            });
                        if (aResult.Success)
                        {
                            LoadDsHocPhi(aResult);
                        }
                        break;
                }
            }
        }

        private void btnIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var rpt = new rptHocPhiBC();
            rpt.DataSource = dgvHocPhi.DataSource;
            var tool = new ReportPrintTool(rpt);
            tool.ShowPreview();
        }

        private void btnRefresh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            LoadDsHocPhi();
            isStatistics = false;
        }

    }
}
